<?php
/**
 * 1 版本需要
 * 实现文件互翻 finish
 *
 * 2 版本需要
 * 可以传入相关参数
 *
 * 3 版本需要
 * 需要解决超时问题
 * 
 * 4 版本需要
 * 做成 joomla 组件
 *
 */
class IniParser
{
	private $languageDirectory = "";			// 语言文件夹路径
	private $sourceLanguageCode = "";			// 来源语言代码
	private $sourceLanguageDirname = "";
	private $targetLanguageCode = "";			// 目标语言代码
	private $targetLanguageDirname = "";
	private $sourceLanguageDirectory = "";		// 来源语言路径
	private $targetLanguageDirectory = "";		// 目标语言路径
	private $needTranslateFileList = array();	// 需要翻译的文件列表

	/**
	 * 构造函数
	 */
	public function __construct($sourceLanguageCode, $sourceLanguageDir, $targetLanguageCode, $targetLanguageDir, $languageDirectory) {
		$this->setLanguageDirectory($languageDirectory);
		$this->setLanguageCode($sourceLanguageCode, $targetLanguageCode);
		$this->setLanguageDir($sourceLanguageDir, $targetLanguageDir);
	}
	
	/**
	 * 设置目录
	 */
	public function setLanguageDir($sourceLanguageDir, $targetLanguageDir) {
		$patten = "/^[a-z]{2}-[A-Z]{2}$|^[a-z]{2}$/";
		if (preg_match($patten, $sourceLanguageDir) > 0 && preg_match($patten, $targetLanguageDir) > 0) {
			$this->sourceLanguageDirname = $sourceLanguageDir;
			$this->targetLanguageDirname = $targetLanguageDir;
		} else {
			throw new Exception("Invalid Language Dir, for example 'zh-CN', 'en-US'");
		}
	}

	/**
	 * 设置目录
	 */
	public function setLanguageDirectory($languageDirectory) {
		str_replace("/", DIRECTORY_SEPARATOR, $languageDirectory);
		str_replace("\\", DIRECTORY_SEPARATOR, $languageDirectory);
		$this->languageDirectory = (substr($languageDirectory, -1) == DIRECTORY_SEPARATOR) ? $languageDirectory : $languageDirectory . DIRECTORY_SEPARATOR;
	}

	/**
	 * 设置语言代码
	 */
	public function setLanguageCode($sourceLanguageCode, $targetLanguageCode) {
		$patten = "/^[a-z]{2}-[A-Z]{2}$|^[a-z]{2}$/";
		if (preg_match($patten, $sourceLanguageCode) > 0 && preg_match($patten, $targetLanguageCode) > 0) {
			$this->setSourceLanguageCode($sourceLanguageCode);
			$this->setTargetLanguageCode($targetLanguageCode);
		} else {
			throw new Exception("Invalid Language Code, for example 'zh-CN', 'en-US'");
		}
	}

	/**
	 * 设置来源语言代码
	 */
	public function setSourceLanguageCode($sourceLanguageCode) {
		$this->sourceLanguageCode = $sourceLanguageCode;
	}

	/**
	 * 设置目标语言代码
	 */
	public function setTargetLanguageCode($targetLanguageCode) {
		$this->targetLanguageCode = $targetLanguageCode;
	}

	/**
	 * 获取语言目录
	 */
	public function getLanguageDirectory() {
		return $this->languageDirectory;
	}

	/**
	 * 获取来源语言代码
	 */
	public function getSourceLanguageCode() {
		return $this->sourceLanguageCode;
	}

	/**
	 * 获取目标语言代码
	 */
	public function getTargetLanguageCode() {
		return $this->targetLanguageCode;
	}

	/**
	 * 初始化
	 */
	public function init() {
		// 测试变量就绪情况
		//echo "<pre>"; var_dump($this->getLanguageDirectory(), $this->getSourceLanguageCode(), $this->getTargetLanguageCode(), $this->sourceLanguageDirname, $this->targetLanguageDirname); die;
		
		// 设置语言路径
		$this->sourceLanguageDirectory = $this->languageDirectory . $this->sourceLanguageDirname . DIRECTORY_SEPARATOR;
		$this->targetLanguageDirectory = $this->languageDirectory . $this->targetLanguageDirname . DIRECTORY_SEPARATOR;
		
		//echo "<pre>"; var_dump($this->sourceLanguageDirectory, $this->targetLanguageDirectory); die;

		// 获得需要翻译的文件列表
		$sourceFiles = $this->getIniFiles($this->sourceLanguageDirectory);
		$targetFiles = $this->getIniFiles($this->targetLanguageDirectory);
		$this->needTranslateFileList = $this->compare($sourceFiles, $targetFiles);
		
		//echo "<pre>"; var_dump($targetFiles); die;
		// 存入 session 中
		session_start();
		$_SESSION['JoomlaIniTranslator'] = $this;
		return $this->needTranslateFileList;
	}

	/**
	 * 获取制定路径下ini文件
	 */
	protected function getIniFiles($path) {
		// 源路径获取
		$di = new DirectoryIterator($path);
		foreach ($di as $k => $d) {	
			if ($d->isFile()) {
				$filename = $d->getFilename();
				$filenameArray = explode('.', $filename);
				if ($filenameArray[count($filenameArray) - 1] == "ini") {
					$files[] = $filename;
				}
			}
		}
		return count($files) > 0 ? $files : array();
	}

	/**
	 * 比较差异文件
	 * 只返回sourcePath中存在，targetPath中不存在的文件结果集
	 */
	protected function compare($sourceFiles, $targetFiles) {
		//echo "<pre>";
		// 比较 用循环嵌套比较
		for ($i = 0; $i < count($sourceFiles); $i++) {
			$strTmp = str_replace($this->sourceLanguageDirname, $this->targetLanguageDirname, $sourceFiles[$i]);
			if (!in_array($strTmp, $targetFiles)) {
				$diffFiles[] = $sourceFiles[$i];
			}
		}
		return $diffFiles;
	}

	public function translateNextFile() {
		$file = array_shift($this->needTranslateFileList);
		$filename = $this->sourceLanguageDirectory.$file;
		$words = $this->read($filename);
		foreach ($words as $k => $w) {
			$t = str_replace('％', '%s', trim($w));
			$f = str_replace('%s', '％', $this->translate($t));
			$stream .= $k . "=" . $f . "\n";
		}
		$targetFileArr = explode('.', $file);
		$count = count($targetFileArr);
		if ($count == 4) {
			$targetFile = $this->targetLanguageDirname.'.'.$targetFileArr[1].'.'.$targetFileArr[2].'.'.$targetFileArr[3];
		} else if ($count == 3) {
			$targetFile = $this->targetLanguageDirname.'.'.$targetFileArr[1].'.'.$targetFileArr[2];
		} else if ($count == 2) {
			$targetFile = $this->targetLanguageDirname.'.'.$targetFileArr[1];
		}
		$targetFilename = $this->targetLanguageDirectory.$targetFile;
		file_put_contents($targetFilename, $stream);
		return true;
	}

	/**
	 * 读取差异文件结果集中的文件内容并返回
	 */
	protected function read($file) {
		$content = file_get_contents($file);
		preg_match_all("/^([^#].+)=(.+)/m", $content, $matches);
		$words = array_combine($matches[1], $matches[2]);
		return $words;
	}
	
	protected function translate($string) {
		$url = "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=".urlencode($string)."&langpair={$this->sourceLanguageCode}%7C{$this->targetLanguageCode}";
		
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		$body = curl_exec($ch);
		curl_close($ch);
		
		$json = json_decode($body);
		return $json->responseData->translatedText;
		//echo "<pre>"; var_dump($json); //die;
	}
}
?>